<?php
//idea of this function is that if you have a str that is kinda long and you want to cap it to a certain number, this will place the ... in the middle of the str rather then at the end
function cap_str_from_middle($str = null, $total_str_len = 90, $str_seperator = "&hellip;")
{
	$str_len = strlen($str);
	$main_count_position = ceil($str_len/2);
	$str_len2 = ($str_len-$total_str_len);
	$strcount_remove_pos = ($str_len2/2);
	
	$main_count_position = ceil($main_count_position-$strcount_remove_pos);
	//echo $main_count_position." - ".$str_len2."<br /><br />";
	
	if($str_len > $total_str_len)
	{
		return substr($str, 0, $main_count_position).$str_seperator.substr($str, ($main_count_position+$str_len2), $str_len);
	}
	else
	{
		return $str;
	}
}

function get_content_type_clean_name($content_type = null)
{
	switch($content_type)
	{
		case "newssmall":
			return "News (1x1 block)";
		break;
		case "newsbig":
			return "News (2x2 blocks)";
		break;
		case "social":
			return "Social";
		break;
		case "worksmall":
			return "Work (1x1 block)";
		break;
		case "worksmallimg":
			return "Work (1x2 block with image)";
		break;
		case "workbig":
			return "Work (2x2 block)";
		break;
		case "workbigsocial":
			return "Work (2x3 block with social)";
		break;
	}
}

function array_multi_sort($array, $cols)
{
    $colarr = array();
    foreach($cols as $col => $order)
    {
        $colarr[$col] = array();
        foreach ($array as $k => $row)
        {
            $colarr[$col]['_'.$k] = strtolower($row[$col]);
        }
    }

    $eval = 'array_multisort(';
    foreach($cols as $col => $order)
    {
        $eval .= '$colarr[\''.$col.'\'],'.$order.',';
    }
    $eval = substr($eval,0,-1).');';
    eval($eval);
    $ret = array();
    foreach($colarr as $col => $arr)
    {
        foreach($arr as $k => $v)
        {
            $k = substr($k,1);
            if (!isset($ret[$k])) $ret[$k] = $array[$k];
            $ret[$k][$col] = $array[$k][$col];
        }
    }
    return $ret;
}

function linkify($tweet) {
	$tweet = preg_replace("/((http(s?):\/\/)|(www\.))([\w\.]+)([a-zA-Z0-9?&%.;:\/=+_-]+)/i", "<a href=\"http$3://$4$5$6\" target=\"_blank\" title=\"Visit $0\">$2$4$5$6</a>", $tweet);
    $tweet = preg_replace("/(?<=\A|[^A-Za-z0-9_])@([A-Za-z0-9_]+)(?=\Z|[^A-Za-z0-9_])/", "<a href=\"http://twitter.com/$1\" target=\"_blank\" title=\"$0 on Twitter\">$0</a>", $tweet);
    $tweet = preg_replace("/(?<=\A|[^A-Za-z0-9_])#([A-Za-z0-9_]+)(?=\Z|[^A-Za-z0-9_])/", "<a href=\"http://twitter.com/search?q=%23$1\" target=\"_blank\" title=\"Search $0 on Twitter\">$0</a>", $tweet);
    return $tweet;
}

function nl2br2($string)
{
	$string = str_replace(array("\r\n", "\r", "\n"), "<br />", $string); 
	return $string; 
} 

function break_string_to_chunks_by_newline($description = null) //this WILL NOT WORK for description that has come from a wysiwyg
{
	$str = nl2br2($description, true);

	$firstParaEnd = strpos($str, "<br />");
	if($firstParaEnd === false)
	{
		$firstParaEnd = 0;
	}
	else
	{
		$firstParaEnd += 6;
		$intro = substr($str, 0, $firstParaEnd);
	}

	$body = substr($str, $firstParaEnd, strlen($str));
	$temp = array("intro" => $intro, "body" => $body);
	return $temp;
}

function recursiveArraySearch($haystack, $needle, $index = null) 
{ 
    $aIt     = new RecursiveArrayIterator($haystack); 
    $it    = new RecursiveIteratorIterator($aIt); 
    
    while($it->valid()) 
    {        
        if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) { 
            return $aIt->key(); 
        } 
        
        $it->next(); 
    } 
    
    return false; 
}

function display_yes_no($value)
{
	return ($value == 1) ? "Yes" : "No";
}

function clean_url($str)
{
  	$temp = stripslashes(strtolower($str));
	$temp = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $temp);
	$temp = strtolower(trim($str, '-'));
	$temp = preg_replace("/[\/_|+ -]+/", '-', $temp);
	return $temp;
}

function clean_url_accents($str, $delimiter = '-')
{
	$temp = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  	$temp = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $temp);
	$temp = strtolower(trim($temp, '-'));
	$temp = preg_replace("/[\/_|+ -]+/", $delimiter, $temp);
	return $temp;
}

function advance_paging($paging, &$min_page, &$max_page, $number_of_links_to_show, $numPages)
{
  //echo $numPages . "<br />";
  if($number_of_links_to_show > $numPages)
  {
    $number_of_links_to_show = $numPages;
  }
  if($paging == 1)
  {
    $min_page = 0;
    $max_page = $number_of_links_to_show;
  }
  if($paging > 1)
  {
    $min_page = $paging - 3;
    $max_page = ($paging + $number_of_links_to_show) - 3;
  }
  if($paging == $numPages)
  {
    $min_page = $paging - 3;
    $max_page = $numPages;
  }
  if($paging >= $numPages)
  {
    $min_page = $paging - $number_of_links_to_show;
    $max_page = $numPages;
  }
  if($max_page > $numPages)
  {
    $max_page = $numPages;
  }
}

/**
 * @desc Outputs a formatted notice box for error or success messages.
 * @param string $notice [error || success]
 * @param string $message  - Message of notice
 */
function output_notice($notice, $message)
{
    $error_css_styling = '<link rel="stylesheet" href="'.ASSETS_CSS.'custom.css"/>';
    echo $error_css_styling; 
    echo '<div class="'.$notice.'">';
        if ($notice == 'error'):
        echo '<h1>Error</h1>';
        endif;
        
        if ($notice == 'success'):
        echo '<h1>Success</h1>';
        endif;
    echo '<hr/>';
    echo $message;
    echo '</div>';
}

function getBrowser() 
{ 
    $u_agent = $_SERVER['HTTP_USER_AGENT']; 
    $bname = 'Unknown';
    $platform = 'Unknown';
    $version= "";

    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    }
    elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    }
    elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }
    
    // Next get the name of the useragent yes seperately and for good reason
    if(preg_match('/MSIE/i',$u_agent) && !preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Internet Explorer'; 
        $ub = "MSIE"; 
    } 
    elseif(preg_match('/Firefox/i',$u_agent)) 
    { 
        $bname = 'Mozilla Firefox'; 
        $ub = "Firefox"; 
    } 
    elseif(preg_match('/Chrome/i',$u_agent)) 
    { 
        $bname = 'Google Chrome'; 
        $ub = "Chrome"; 
    } 
    elseif(preg_match('/Safari/i',$u_agent)) 
    { 
        $bname = 'Apple Safari'; 
        $ub = "Safari"; 
    } 
    elseif(preg_match('/Opera/i',$u_agent)) 
    { 
        $bname = 'Opera'; 
        $ub = "Opera"; 
    } 
    elseif(preg_match('/Netscape/i',$u_agent)) 
    { 
        $bname = 'Netscape'; 
        $ub = "Netscape"; 
    } 
    
    // finally get the correct version number
    $known = array('Version', $ub, 'other');
    $pattern = '#(?<browser>' . join('|', $known) .
    ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }
    
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent,"Version") < strripos($u_agent,$ub)){
            $version= $matches['version'][0];
        }
        else {
            $version= $matches['version'][1];
        }
    }
    else {
        $version= $matches['version'][0];
    }
    
    // check if we have a number
    if ($version==null || $version=="") {$version="?";}
    
    return array(
        'userAgent' => $u_agent,
        'name'      => $bname,
        'version'   => $version,
        'platform'  => $platform,
        'pattern'    => $pattern,
		'short_name' => $ub
    );
} 
?>